package api

import (
	"awesomeProject2/global"
	"awesomeProject2/model"
	"awesomeProject2/model/request"
	"awesomeProject2/model/response"
	"awesomeProject2/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"net/http"
	"strconv"
	"strings"
)

type Achievement struct {
	gorm.Model
	AchievementLevel    string   `json:"achievementLevel"`
	AchievementName     string   `json:"achievementName"`
	AchievementCategory string   `json:"achievementCategory"`
	AchievementFrom     string   `json:"achievementFrom"`
	AchievementFile     []string `json:"achievementFile"`
	AchievementRanking  int      `json:"achievementRanking"`
	Year                int      `json:"year"`
}

func CreateAchievement(c *gin.Context) {
	var data Achievement
	_ = c.ShouldBindJSON(&data)
	userid, _ := c.Get("userid")

	var achievement model.Achievement
	achievement.ID = data.ID
	//Year := model.GetYear()
	achievement = model.Achievement{
		AchievementLevel:    data.AchievementLevel,
		AchievementName:     data.AchievementName,
		AchievementCategory: data.AchievementCategory,
		AchievementFrom:     data.AchievementFrom,
		AchievementRanking:  data.AchievementRanking,
		AchievementFile:     strings.Join(data.AchievementFile, ","),
		UserId:              userid.(uint),
		Year:                data.Year,
	}

	if err := service.CreateAchievement(achievement); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":    "添加失败",
			"status": http.StatusNotFound,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "添加成功",
				"status": http.StatusOK,
			},
		})
	}
}

func GetAchievement(c *gin.Context) {
	acid, _ := strconv.Atoi(c.Param("id"))
	if err, data := service.GetAchievement(uint(acid)); err != nil {

		c.JSON(http.StatusOK, gin.H{"msg": "查询失败", "status": http.StatusNotFound})
	} else {
		//data, _ := json.Marshal(reachievement)
		c.JSON(http.StatusOK, gin.H{
			"data": data,
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		},
		)
	}
}
func GetAchievements(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	userid, _ := strconv.Atoi(c.Query("userid"))
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = 1
	}

	if err, achievements, total := service.GetAchievements(uint(userid), pageSize, pageNum); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "查询失败", "status": http.StatusNotFound})

	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  gin.H{"achievementList": achievements},
			"tatal": total,
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		},
		)
	}
}

// UpdateAchievement 更新
func UpdateAchievement(c *gin.Context) {
	var data Achievement
	_ = c.ShouldBindJSON(&data)
	//Year := model.GetYear()
	achievement := model.Achievement{
		AchievementLevel:    data.AchievementLevel,
		AchievementName:     data.AchievementName,
		AchievementCategory: data.AchievementCategory,
		AchievementFrom:     data.AchievementFrom,
		AchievementRanking:  data.AchievementRanking,
		AchievementFile:     strings.Join(data.AchievementFile, ","),
		Year:                data.Year,
	}
	achievement.ID = data.ID

	if err := service.UpdateAchievement(achievement); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "更新失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "更新成功",
				"status": http.StatusOK,
			},
		})
	}
}
func DeleteAchievement(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := service.DeleteAchievement(uint(id)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})

	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "删除成功",
				"status": http.StatusOK,
			},
		})
	}
}
func ChangeAchievementVisiblity(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := service.ChangeAchievementVisiblity(uint(id)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "更新失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "更新成功",
				"status": http.StatusOK,
			},
		})
	}
}

func GetAllAchievements(c *gin.Context) {
	userid, _ := c.Get("userid")
	year, _ := strconv.Atoi(c.Param("year"))
	if err, reachievement, tt := service.GetAllAchievementsByYear(userid.(uint), year); err != nil {

		c.JSON(http.StatusOK, gin.H{"msg": userid.(uint), "status": http.StatusNotFound})
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"achievementList": reachievement, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		},
		)
	}
}
func GetAllAchievementsWithouYear(c *gin.Context) {
	userid, _ := c.Get("userid")
	if err, reachievement, tt := model.GetAllAchievements(userid.(uint)); err != nil {

		c.JSON(http.StatusOK, gin.H{"msg": userid.(uint), "status": http.StatusNotFound})
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"achievementList": reachievement, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		},
		)
	}
}

func GetAchievementsDynamic(c *gin.Context) {
	var searchinfo request.AchievementSearchInfo
	_ = c.ShouldBindQuery(&searchinfo)
	if err, list, total := service.GetAchievementsDynamic(searchinfo); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.Fail(c)
	} else {
		response.OkWithData(response.PageResult{
			Total: total,
			List:  list,
		}, c)
	}
}
